package org.freez4.spring.bean.definition;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * Bean 的初始化方式
 * 常规方式
 * 1. 通过构造器(配置元信息: XML、Java 注解和Java API) {@link AnnotationBeanDefinitionDemo}
 * 2. 通过静态工厂方法(配置元信息:XML 和 Java API)
 * 3. 通过 Bean工厂方法(配置元信息:XML 和 Java API)
 * 4. 通过 FactoryBean(配置元信息:XML、Java注解和Java API)
 * <p>
 * 特殊方式
 * 1. 通过 ServiceLoaderFactoryBean(配置元信息:XML、Java注解和Java API)
 * 2. 通过 AutowireCapableBeanFactory#createBean(java.lang.Class,int,boolean)
 * 3. 通过 BeanDefinitionRegistry#registryBeanDefinition(String,BeanDefinition)
 *
 * @author 阿坤同学
 * @date 2023/12/6 4:34 PM
 */
public class BeanInstantiationDemo {

    private static final Logger logger = Logger.getLogger(BeanInstantiationDemo.class.getName());

    public static void main(String[] args) {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:/META-INF/bean-instantiation-context.xml");
        logger.log(Level.INFO, () -> "通过静态方法初始化Bean" + applicationContext.getBean("user-by-static-method"));
        logger.log(Level.INFO, () -> "通过静态工厂方法初始化Bean" + applicationContext.getBean("user-by-instance-method"));
        logger.log(Level.INFO, () -> "通过FactoryBean初始化Bean" + applicationContext.getBean("user-by-factory-bean"));
    }

}